package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestPermission;
import com.kgc.scd.annotation.RequestUser;
import com.kgc.scd.bean.Cart;
import com.kgc.scd.bean.CartItem;
import com.kgc.scd.service.CartService;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.UserAnno;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created On : 2022/6/11.
 * <p>
 * Author     : 98730
 * <p>
 * Description: 购物车业务操作入口
 */
@RestController
@Slf4j
@Api(tags = "17、购物车操作入口")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * @author : 98730
     * @date   : 2022/6/11
     * @param  : [com.kgc.scd.bean.Cart, com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 添加购物车
     */
    @PostMapping("/addCart")
    @RequestPermission
    @ApiOperation(value = "添加购物车",notes = "根据用户id添加购物车")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<String> addCart(@RequestBody Cart cart,@RequestUser UserAnno userAnno){
        log.info("web-添加购物车:{}", cart);
        cart.setCartUid(userAnno.getUserId());
        return cartService.addCart(cart);
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.util.List<java.lang.Integer>, com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 删除购物车
     */
    @PostMapping("/delCart")
    @RequestPermission
    @ApiOperation(value = "删除购物车",notes = "根据用户选择购物车id删除购物车内容")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<String> delCart(@RequestParam List<Integer> cartId, @RequestUser UserAnno user){
        log.info("web-删除购物车, 用户为：{}", user);
        return cartService.delCart(cartId,user);
    }

    /**
     * @author : 98730
     * @date   : 2022/6/10
     * @param  : []
     * @return : com.kgc.scd.util.RequestResult<java.util.List<com.kgc.scd.bean.Cart>>
     * @description : 显示购物车列表，每页3条数据
     */
    @PostMapping("/queryCart")
    @RequestPermission
    @ApiOperation(value = "显示用户购物车列表",notes = "显示用户购物车内容-带分页")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<PageSupport<CartItem>> queryCartListByPage(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "3") Integer pageSize, @RequestUser UserAnno user){
        return cartService.queryCartListByPage(pageNo,pageSize,user);
    }

    /**
     * @author : 98730
     * @date   : 2022/6/13
     * @param  : [java.lang.String, java.lang.Integer, com.kgc.scd.util.UserAnno]
     * @return : com.kgc.scd.util.RequestResult<java.lang.String>
     * @description : 修改购物车
     */
    @PostMapping("/modCartList")
    @RequestPermission
    @ApiOperation(value = "更新购物车商品数量",notes = "根据用户输入的商品数量，修改购物车")
    @ApiResponses({
            @ApiResponse(code = 200, message = "SUCCESS"),
            @ApiResponse(code = 500, message = "FAIL")
    })
    public RequestResult<String> modCartList(@RequestParam String proId, @RequestParam(defaultValue = "1") Integer count, @RequestUser UserAnno userAnno){
        return cartService.modCartList(proId,count,userAnno);
    }
}
